<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;

class article extends Controller
{
    /**
     * @param $id是文章ID
     * @return \think\response\View
     */
    public function index($id)
    {
        //登录处理
        $info = Session::get('user_info');

        $user_id = $info['user_id'];
        $art = Db::name('artcomment')->where('user_id', $user_id)->select();
        $posts = Db::name('post')->where('user_id', $user_id)->select();


        $session = Session::get('user_info');
        $list = Db::name('article')->find($id);

        $artcom = Db::name('artcomment')->where('art_id',$id)->select();
        $colname = Db::name('column')->field('colname')->where('column_id',$list['column_id'])->select();
        $count = Db::name('artcomment')->where('art_id',$id)->count();

        $artlist = array();
        //文章评论表
        foreach($artcom as $val)
        {
            $user = Db::name('index_user')->where('user_id',$val['user_id'])->find();
            $runcomment = Db::name('runcomment')->where('artcom_id',$val['artcom_id'])->select();

            $val['username'] = $user['username'];
            $val['userpic'] = $user['userpic'];
            $rundata = array();
            //文章追评表
            foreach($runcomment as $v){
                $user = Db::name('index_user')->where('user_id',$val['user_id'])->find();
                $v['runcomuser'] = $user['username'];
                $rundata[] = $v;
            }
                $val['runcom'] = $rundata;//把追评信息放入$artcom数组中
                $artlist[] =$val;
        }
        //浏览加一
        // var_dump($artlist);
        // die;

        $data = Db::name('article')->find($id);
        $views = $data['views'];
        $views = $views + 1;
        $postdata = [
            'views' => $views
        ];

        Db::name('article')->where('art_id',$id)->update($postdata);

        $love = Db::name('article')->limit(5)->order('artlove desc')->select();
        $datas = array();
        foreach($love as $v){
            $colname_hot = Db::name('column')->field('colname')->where('column_id',$v['column_id'])->find();
            $v['colname_hot'] = $colname_hot;
            $datas[] = $v;
        }
        $links = Db::name('links')->select();

        return view('article/index',[
            'love'=>$datas,
            'lists'=>$list,//文章数据
            'session' => $session,//用户缓存
            'links'=>$links,
            'colname' => $colname,//栏目h 
            'count' => $count,//评论数量
            'artcom' => $artlist,//用户文章评论(包括追评)
            'user_id' => $info['user_id'],
            'username' => $info['username'],
            'sex' => $info['sex'],
            'age' => $info['age'],
            'email' => $info['email'],
            'userpic' => $info['userpic'],
            'tel' => $info['tel'],
            'integral' => $info['bp'],
            'list' => $art,
            'posts' => $posts,

        ]);
    }


    public function addcoment()
    {
        $session = Session::get('user_info');
        $data = [
            'art_id' => $_POST['art_id'],
            'user_id' => $session['user_id'],
            'artcontent' => $_POST['com'],
            'artcomtime' => date('Y-m-d h:m:s')
        ];

        //发评论增加积分
        $user = Db::name('index_user')->find($session['user_id']);
        $bp = $user['bp'] + 3;
        $userdata = [
            'bp' => $bp
        ];
        Db::name('index_user')->where('user_id',$session['user_id'])->update($userdata);
        //发评论增加积分

        $result = Db::name('artcomment')->insert($data);
        $users = Db::name('index_user')->find($session['user_id']);
        if($result){
            $info['status'] = true;
            Session::delete('user_info');
            Session::set('user_info',$users);
        }else{
            $info['status'] = false;
        }
        // $info['status'] = true;
        return json($info);
    }

    public function addartoment()
    {
        $session = Session::get('user_info');
        $data = [
            'runcontent' => $_POST['runcontent'],
            'art_id' => $_POST['art_id'],
            'artcom_id' => $_POST['artcom_id'],
            'user_id' => $session['user_id']

        ];

        //发评论增加积分
        $user = Db::name('index_user')->find($session['user_id']);
        $bp = $user['bp'] + 3;
        $userdata = [
            'bp' => $bp
        ];
        Db::name('index_user')->where('user_id',$session['user_id'])->update($userdata);
        //发评论增加积分
        
        $result = Db::name('runcomment')->data($data)->insert();
        $users = Db::name('index_user')->find($session['user_id']);
        if($result){
            $info['status'] = true;
            Session::delete('user_info');
            Session::set('user_info',$users);
        }else{
            $info['status'] = false;
        }

        return json($info);
    }


    //文章点赞的ajax传值
    function artlove()
    {

       
        
        $data = Db::name('article')->find($_POST['artid']);;
        // var_dump($data);
        // die;
        $love = $data['artlove'] + 1 ;
        $artdata = [
            'artlove' => $love
        ];
        // var_dump($artdata);
        $result = Db::name('article')->where('art_id',$_POST['artid'])->update($artdata);
        if($result){
            $info['status'] = true;
            $info['love'] = $love;

        }else{
            $info['status'] = false;
        }
        return $info;


    }

    //帖子评论点赞的ajax传值
    function comlove()
    {

        $data = Db::name('artcomment')->find($_POST['comid']);
        // var_dump($data);
        // die;
        $love = $data['artcomlove'] + 1 ;
        $comdata = [
            'artcomlove' => $love
        ];
        
        $result = Db::name('artcomment')->where('artcom_id',$_POST['comid'])->update($comdata);
        // var_dump($result);
        // die;
        if($result){
            $info['status'] = true;
            $info['love'] = $love;

        }else{
            $info['status'] = false;
        }
        return $info;


    }
    //帖子评论讨厌的ajax传值
    function comhit()
    {
        // var_dump($_POST);
        // die;
        $data = Db::name('artcomment')->find($_POST['comid']);
        $hit = $data['artbcomhit'] + 1 ;
        $comdata = [
            'artbcomhit' => $hit
        ];
        
        $result = Db::name('artcomment')->where('artcom_id',$_POST['comid'])->update($comdata);
        // var_dump($result);
        // die;
        if($result){
            $info['status'] = true;
            $info['hit'] = $hit;

        }else{
            $info['status'] = false;
        }
        return $info;
    }
}
